package com.haoshuang.zhouzhoubang.ui.page.welfare.insure.apply

import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.RecyclerView
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ItemInsureApplyBinding
import com.haoshuang.zhouzhoubang.widget.CustomUploadView
import com.haoshuang.zhouzhoubang.widget.UploadFileBean
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.utils.view.imageResource
import com.mufeng.libs.utils.view.load


/**
 * 
 */
class InsureApplyAdapter(data: MutableList<InsureApplyBean>) : BaseBindingQuickAdapter<InsureApplyBean, ItemInsureApplyBinding>(data) {

    override fun onBindViewHolder(holder: BaseBindingHolder, position: Int, item: InsureApplyBean?) {
        holder.getViewBinding<ItemInsureApplyBinding>().apply {
            ivAddMinus.imageResource = if(position == 0) R.mipmap.icon_insure_add else R.mipmap.icon_insure_minus
            etAccount.tag = position
            etName.tag = position
            etPhone.tag = position
            etAccount.setText(items[etAccount.tag as Int].account)
            etName.setText(items[etName.tag as Int].real_name)
            etPhone.setText(items[etPhone.tag as Int].mobile)
            ivIDCard.setSingleMedia(item?.card_front)
            ivIDCardBack.setSingleMedia(item?.card_front)

            ivIDCard.setOnUploadResultListener(object: CustomUploadView.OnUploadResultListener{
                override fun onUploadResult(mediaList: List<UploadFileBean>) {
                    item?.card_front = mediaList[0].url?:""
                }
            })

            ivIDCardBack.setOnUploadResultListener(object: CustomUploadView.OnUploadResultListener{
                override fun onUploadResult(mediaList: List<UploadFileBean>) {
                    item?.card_side = mediaList[0].url?:""
                }
            })

            etAccount.addTextChangedListener {
                val tag = etAccount.tag as Int
                items[tag].account = etAccount.text.toString()
            }

            etName.addTextChangedListener {
                val tag = etName.tag as Int
                items[tag].real_name = etName.text.toString()
            }

            etPhone.addTextChangedListener {
                val tag = etPhone.tag as Int
                items[tag].mobile = etPhone.text.toString()
            }
        }
    }

}